﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Collections.Specialized;
using Extentions;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Storage;
using TrainingReminderSystem.Dal;
using TrainingReminderSystem.Model;
using Microsoft.EntityFrameworkCore;

namespace TrainingReminderSystem.Bll
{
    /// <summary>
    /// 逻辑层
    /// </summary>
    public partial class SysDicBll : PcLoginedBll
    {
        #region 排序

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="idsList"></param>
        /// <returns></returns>
        public AjaxResult<int> Order(List<int> idsList)
        {
            using (var db = Db)
            {
                var modelList = db.SysDic.Where(t => idsList.Contains(t.SysDicID)).ToList();

                int index = 0;
                foreach (int i in idsList)
                {
                    index++;
                    var model = modelList.FirstOrDefault(t => t.SysDicID == i);
                    model.OrderID = index;
                }

                db.SaveChanges();
                return AjaxResult<int>.SuccessResult(0);
            }
        }

        #endregion

        #region 查询集合方法

        /// <summary>
        /// 查询集合方法
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public AjaxResult<dynamic> GetListSelect(IFormCollection form)
        {
            using (var db = Db)
            {
                Expression<Func<SysDic, bool>> where = InitSearch<SysDic>(form, InitSearchDelegete); //拼接查询条件方法
                //if (CurrentStore != null)
                //{
                //    int storeID = CurrentStore.SysStoreID;
                //    where = where.ExAnd(m => m.SysStoreID == storeID);
                //}

                EnumDicType enumDicType = (EnumDicType)form["EnumDicType"].ExToInt32();
                where.ExAnd(m => m.EnumDicType == enumDicType);
                var selectID = form["selectIDName"].ExTrim();
                var obj = db.SysDic.AsNoTracking();
                bool hasSelectID = false;
                if (!string.IsNullOrEmpty(selectID))
                {
                    if (selectID.IndexOf(',') != -1)
                    {
                        var selectIDList = selectID.ExToSplitList();
                        if (selectIDList.Count > 0)
                        {
                            hasSelectID = true;
                            obj = obj.OrderByDescending(m => selectIDList.Contains(m.SysDicID))
                                .ThenBy(m => m.OrderID).ThenBy(m => m.SysDicID);
                        }
                    }
                    else
                    {
                        var selectIDInt = selectID.ExToInt32();
                        if (selectIDInt > 0)
                        {
                            hasSelectID = true;
                            obj = obj.OrderByDescending(m => selectIDInt == m.SysDicID)
                                .ThenBy(m => m.OrderID).ThenBy(m => m.SysDicID);
                        }
                    }

                    if (hasSelectID == false)
                    {
                        where = where.ExAnd(m => m.SysDicName.Contains(selectID) || m.PY.Contains(selectID));
                    }
                }

                if (hasSelectID == false)
                {
                    obj = obj.OrderBy(m => m.OrderID).ThenBy(m => m.SysDicID);
                    //where = where.ExAnd(m => m.EnumStatus == EnumStatus.启用);
                }
                var result = new AjaxResult<dynamic>() { Success = true };
                var list = obj.Where(where).Take(DefaultSize);
                var tempList = list.Select(m => new
                {
                    SysDicID = m.SysDicID,
                    SysDicName = m.SysDicName
                });
                result.Result = tempList.ToList();
                return result;
            }
        }

        #endregion

        #region 查询集合方法

        /// <summary>
        /// 查询集合方法
        /// </summary>
        /// <param name="enumDicType"></param>
        /// <param name="top">默认条数</param>
        /// <returns></returns>
        public AjaxResult<dynamic> GetListTextValue(EnumDicType enumDicType, int top = 50)
        {
            using (var db = Db)
            {
                Expression<Func<SysDic, bool>> where = m => m.EnumDicType == enumDicType;
                var result = new AjaxResult<dynamic>() { Success = true };
                IQueryable<SysDic> list = db.SysDic.OrderBy(m => m.OrderID).ThenBy(m => m.SysDicID).Where(where).OrderBy(m => m.OrderID).Take(top);
                result.Result = list.Select(m => new
                {
                    value = m.SysDicID,
                    text = m.SysDicName
                }).ToList();
                return result;
            }
        }

        #endregion
    }
}
